<?php
include 'AdminController.php';
class Admin_OrderController extends Admin_AdminController
{
    public function init(){
    	//Sử dụng admin layout
        $layoutPath = APPLICATION_PATH . '/templates/admin/admin';
        $option = array('layout' => 'index', 'layoutPath' => $layoutPath);
        Zend_Layout::startMvc($option);
        
        //アクティブメニューを設定
        $this->view->active_menu = "orders";
    }
	
	public function indexAction(){
		//TODO 必要なクラスを読み込む
    	Zend_Loader::loadClass("UserModel");
    	Zend_Loader::loadClass("TableModel");
    	Zend_Loader::loadClass("OrderModel");
    	Zend_Loader::loadClass("SelectListModel");
    	
    	$request = $this->getRequest();
    	$view = $this->view;
    	
    	//TODO パラメタを取得
    	$start_time = $request->getParam('start_time');
    	$end_time = $request->getParam('end_time');
    	$status = $request->getParam("status");
    	$room = $request->getParam("room");
    	$page = $request->getParam('page',1);
    	
    	
    	//TODO ユーザの予約の一覧を読み出すためにオーだモデルのインスタンスを作成する
    	$model = new OrderModel();
    	$data = $model->getListOrder($start_time, $end_time, $status, $page, 10,$room);
    	$data_count = $model->getListOrderCount($start_time, $end_time, $status,$room);
    	
    	//TODO データを一覧に表現するためにテーブルモデルを作成
    	$table_model = new TableModel();
    	$table_model->setTable_title("予約参照");
    	
    	//テーブルヘッダを定義する
    	$table_model->addHeader('id', '予約ID', true);
		$table_model->addHeader('ordered_time', '登録時間', true);
    	$table_model->addHeader('username', 'テナントさん', false);
    	$table_model->addHeader('code', '会議室', true);
    	$table_model->addHeader('rent_price', '使用料/30分', false);
    	$table_model->addHeader('start_time', '始め時間', false);
    	$table_model->addHeader('end_time', '終わり時間', false);
    	$table_model->addHeader('rent_total', '料金', false);
		$table_model->addHeader('status', '状態', false);

    	//TODO テーブルのデータを設定
    	$table_model->setTable_data($data);
    	$table_model->setData_count($data_count);
    	$table_model->setPage($page);
    	$table_model->setLine_per_page(10);
    	
    	//TODO テーブルのリンクを設定
    	$table_model->setSearch_link("/admin/order");
    	
    	//他のリック
		$more_links = array('/user/order/cancel'=> 'キャンセル');
		$table_model->setMore_links($more_links);
    	
    	//マルチアクション設定
    	//$table_multi_actions = array('cancel' => 'キャンセル');
    	//$table_model->setTable_multi_actions($table_multi_actions);
    	
    	$view->table = $table_model;
    	$status_stranslate = array(
    	    STATUS_DELETED => '削除した',
    		STATUS_NORMAL => '登録した',
    		STATUS_CANCELED => 'キャンセルされた',
    		STATUS_PENALTY => 'ペナルティ'
    	);
    	$table_translate['status'] = $status_stranslate;
    	$view->table_translate = $table_translate;
    	
    	//TODO AJAXで検索場合
    	if($request->getParam('search')){
    		//disable layout
    		$layout = $this->_helper->layout();
		    $layout->disableLayout();
		    
		    $view->search_mode = 1;
    	}
    	
    	//通知
    	$view->msg = $request->getParam('msg');
    	$view->order_status_list = SelectListModel::getListOrderStatus();
    	$view->room_code_list = SelectListModel::getListRoom(null,true);
    	
    	//TODO Navigator を設定
    	$nav['ホーム'] = "/admin";
    	$nav['予約参照'] = "/admin/order";
    	$view->nav = $nav;
	}    
    
	function penaltyAction(){
		//TODO 必要なクラスを読み込む
    	Zend_Loader::loadClass("OrderModel");
    	Zend_Loader::loadClass("SelectListModel");
    	Zend_Loader::loadClass("PaymentHistoryModel");
    	
    	$request = $this->getRequest();
    	$view = $this->view;
    	
    	//TODO パラメターを習得
    	$user_id = $request->getParam("user_id");
    	$room_id = $request->getParam("room_id");
    	$start_time = $request->getParam("start_date") . " " . $request->getParam("start_time") . ":00";
    	$end_time = $request->getParam("end_date") . " " . $request->getParam("end_time") . ":00";
    	
    	if($request->isPost()){
    		//TODO ペナルティ
    		$model = new OrderModel();
    		$orderd_time = date('Y-m-d H:i:s', time());
    		
    		$check = $model->addOrder($user_id, $room_id, $orderd_time, $start_time, $end_time, PENALTY_MONEY, STATUS_PENALTY);
    		
    		//fill payment history
			$payment_history_model = new PaymentHistoryModel();
			$payment_history_model->addFeeForMonths($user_id, $start_time, $end_time, PENALTY_MONEY);
			$payment_history_model->addOrderCount(substr($start_time, 0, 7), $user_id, 1);
    		
    		if($check){
				$this->_redirect('/admin/order?msg=ペナルティを追加しました。');
    		}else{
    			$this->_redirect('/admin/order?msg=ペナルティを追加できません。');
    		} 
    	}
    	
    	//TODO Navigator を設定
    	$nav['ホーム'] = "/admin";
    	$nav['ユーザの一覧'] = "/admin/user";
    	$nav['ペナルティ'] = "/admin/user/penalty";
    	$view->nav = $nav;
    	
    	$view->room_list = SelectListModel::getListRoom(NULL, false, false);
	}
	
	
	/** Call by ajax **/
    public function deleteAction ()
    {
        Zend_Loader::loadClass("OrderModel");

        $request = $this->getRequest();
        $model = new OrderModel();
        $del_ids = $request->getParam("del_ids");
        if ($del_ids) {
            if ($model->deleteOrder($del_ids))
                echo "success";
            else
                echo "error";
        } else {
            echo "not_found_id";
        }
        exit();
    }
    
	/** Call by ajax **/
    public function updateStatusAction ()
    {
        Zend_Loader::loadClass("OrderModel");

        $request = $this->getRequest();
        $model = new OrderModel();
        
        if($request->isPost()){
	        $order_id = $request->getParam("order_id");
	        $status = $request->getParam("status");
	        
	        if ($order_id && $status) {
	        	$data = array('status' => $status);
	            $model->updateOrderData($order_id, $data);
	        } else {
	        	echo "not_found_id";
	        }
        }
        
        exit();
    }
}